<template>
	<view>
		<view class="view-scroview-container">
			<!-- 一级分类 -->
			<scroll-view scroll-y="true" class="view-scroview-left" :style="{height: wh + 'px'}">
			<block v-for="(item,i) in cateList" :key="i">
				<view :class="['view-scroview-left-item', i === active ? 'active' : '']" @click="activeChange(i)">
					{{item.cat_name}}
				</view>
			</block>
			</scroll-view>
			
			<scroll-view scroll-y="true" class="view-scroview-right":style="{height: wh + 'px'}" :scroll-top="scrollTop">
				<!-- 二级分类 -->
				<view class="cate-lev2-list">
					<view class="cate-lev2-item" v-for="(item2,i2) in cateLevel2List" :key="i2">
						<view class="cate-lev2-title">
							/{{item2.cat_name}}/
						</view>
					  <!-- 三级分类 -->
					  <view class="cate-lv3-list">
					  	<view class="cate-lv3-item" v-for="(item3, i3) in item2.children" :key="i3" @click="gotoGoodList(item3)">
					  		<image :src="item3.cat_icon"></image>
					  		<text>{{item3.cat_name}}</text>
					  	</view>
					  </view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				wh:0,
				cateList:[],  // 一级分类数组
				active:0,
				cateLevel2List:[],// 二级分类数组
				scrollTop:0
			};
		},
		onLoad() {
			const sysinfo = uni.getSystemInfoSync()
			this.wh = sysinfo.windowHeight
			this.getCateList()
		},
		methods:{
			async getCateList(){
				const {data:res} = await uni.$http.get('/api/public/v1/categories')
				console.log(res)
				if(res.meta.status!==200) return uni.$showMsg()
				this.cateList = res.message
				this.cateLevel2List = res.message[0].children
			},
			// 点击一级分类
			activeChange(i){
				this.active = i
				this.cateLevel2List = this.cateList[i].children
				this.scrollTop = this.scrollTop === 0 ? 1 : 0
				 //解决点击一级分类时三级分类滚动位置问题，使其滚动到顶部
			},
			// 三级分类跳转商品列表
			gotoGoodList(item){
				uni.navigateTo({
					url:'/subpkg/goods_list/goods_list?cid='+item.cat_id
				})
			}
		}
	}
</script>

<style lang="scss">
.view-scroview-container{
	display: flex;
	.view-scroview-left{
		width: 120px;
		.view-scroview-left-item{
			line-height: 60px;
			text-align: center;
			font-size: 12px;
					background-color: #FFFFFF;
					position: relative;
				&.active {
				  background-color: #FFFFFF;
				  position: relative;
				
				  &::before {
				    content: ' ';
				    display: block;
				    width: 3px;
				    height: 30px;
				    background-color: #C00000;
				    position: absolute;
				    top: 50%;
				    left: 0;
				    transform: translateY(-50%);
				  }
				}
			}
	  }
}
.cate-lev2-item{
	font-size: 16px;
	font-weight: bold;
	text-align: center;
}
.cate-lv3-list{
	display: flex;
	flex-wrap: wrap;
	.cate-lv3-item{
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		margin-bottom: 10px;
		width: 33.33%;
		background-color: #C00000;
		image{
			width: 60px;
			height: 60px;	
					background-color: antiquewhite;
		}
		text{
			font-size: 12px;
		}
	}
}
</style>
